package com.hz.common.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;

import java.lang.reflect.Method;

public final class Utils {

    @SuppressLint("StaticFieldLeak")
    private static Application sApp;

    private Utils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }


    /**
     * Return the Application object.
     * <p>Main process get app by UtilsFileProvider,
     * and other process get app by reflect.</p>
     *
     * @return the Application object
     */
    public static Application getApp() {
        try {
            if (sApp == null) {
                Class<?> aClass = Class.forName("android.app.ActivityThread");
                Method classMethod = aClass.getMethod("currentApplication");
                sApp = (Application) classMethod.invoke(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sApp;
    }

    ///////////////////////////////////////////////////////////////////////////
    // interface
    ///////////////////////////////////////////////////////////////////////////

    public abstract static class Task<Result> extends ThreadUtils.SimpleTask<Result> {

        private final Consumer<Result> mConsumer;

        public Task(final Consumer<Result> consumer) {
            mConsumer = consumer;
        }

        @Override
        public void onSuccess(Result result) {
            if (mConsumer != null) {
                mConsumer.accept(result);
            }
        }
    }

    public interface OnAppStatusChangedListener {
        void onForeground(Activity activity);

        void onBackground(Activity activity);
    }

    public static class ActivityLifecycleCallbacks {

        public void onActivityCreated(Activity activity) {/**/}

        public void onActivityStarted(Activity activity) {/**/}

        public void onActivityResumed(Activity activity) {/**/}

        public void onActivityPaused(Activity activity) {/**/}

        public void onActivityStopped(Activity activity) {/**/}

        public void onActivityDestroyed(Activity activity) {/**/}
    }

    public interface Consumer<T> {
        void accept(T t);
    }

    public interface Supplier<T> {
        T get();
    }

    public interface Func1<Ret, Par> {
        Ret call(Par param);
    }
}
